<?php

namespace app\api\controller;

use app\BaseController;
use app\api\model\Favorite as FavoriteModel;
use app\api\model\Article as ArticleModel;
use app\api\model\Voice as VoiceModel;
use app\api\model\Video as VideoModel;

class Favorite extends BaseController
{
    public function initialize()
    {
        $this->uid = $this->request->uid;
    }

    /* 我的收藏列表 */
    public function list()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $page = input('page', 1, 'intval');
        $perpage = 12;
        $result = FavoriteModel::where('uid', $this->uid)->order('time desc')->page($page, $perpage)->select()->toArray();
        if ($result) {
            $data = [];
            foreach ($result as $v) {
                if ($v['type'] == 1) {
                    $one = VideoModel::where('id', $v['rid'])->field('title, profile')->find()->toArray();
                    $v['cover'] = $one['profile'];
                } elseif ($v['type'] == 2) {
                    $one =  VoiceModel::where('id', $v['rid'])->field('title, profile')->find()->toArray();
                    $v['cover'] = $one['profile'];
                } elseif ($v['type'] == 3) {
                    $one = ArticleModel::where('id', $v['rid'])->field('title, cover')->find()->toArray();
                    $v['cover'] = $one['cover'];
                }
                $v['title'] = $one['title'];
                $data[] = $v;
            }
            return show(200, '我的收藏', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 收藏、取消收藏 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $type = input('type', 0, 'intval');
        $rid = input('rid', 0, 'intval');
        $one = FavoriteModel::where(['uid' => $this->uid, 'type' => $type, 'rid' => $rid])->find();

        if (!$one) {
            FavoriteModel::create([
                'uid' => $this->uid,
                'type' => $type,
                'rid' => $rid,
                'time' => time()
            ]);

            if ($type == 1) {
                $favorite = VideoModel::where('id', $rid)->value('favorite');
                VideoModel::update(['favorite' => $favorite + 1], ['id' => $rid]);
            } else if ($type == 2) {
                $favorite = VoiceModel::where('id', $rid)->value('favorite');
                VoiceModel::update(['favorite' => $favorite + 1], ['id' => $rid]);
            } else if ($type == 3) {
                $favorite = ArticleModel::where('id', $rid)->value('favorite');
                ArticleModel::update(['favorite' => $favorite + 1], ['id' => $rid]);
            }
            return show(200, '收藏成功');
        } else {
            FavoriteModel::where(['uid' => $this->uid, 'type' => $type, 'rid' => $rid])->delete();

            if ($type == 1) {
                $favorite = VideoModel::where('id', $rid)->value('favorite');
                VideoModel::update(['favorite' => $favorite - 1], ['id' => $rid]);
            } else if ($type == 2) {
                $favorite = VoiceModel::where('id', $rid)->value('favorite');
                VoiceModel::update(['favorite' => $favorite - 1], ['id' => $rid]);
            } else if ($type == 3) {
                $favorite = ArticleModel::where('id', $rid)->value('favorite');
                ArticleModel::update(['favorite' => $favorite - 1], ['id' => $rid]);
            }
            return show(200, '取消收藏');
        }
    }
}
